<?php
/**
 * 学校图片管理模块
 * 
 + ----------------------------------------------------- 
 * Date 2013-3-8
 + -----------------------------------------------------
 */
class AttachAction extends CommonAction {
	
	public function index(){
		$map=array();
		$model=M('Attach');
		$map['record_id']=intval($_REQUEST['record_id']);
		$map['is_pic']	=	1;
		$map['model']	=	'Article';
		$this->_list($model,$map);
		$this->display();
	}
	
	public function add() {
		$this->display();
	}
	
	public function insert(){
		if(empty($_POST['name'])){
			$this->error('请填写图片名称！');
		}
		//若有上传文件
		if(!empty($_FILES['image']['name'])){
			//图片上传 自定义目录
			$info=$this->_upload($this->getSavePath($_FILES['image']['name'],3,'Article'),false);
			//图片存入
			$image = isset($info[0]['savename']) ? $info[0]['savepath'].$info[0]['savename']: '';
		}else{
			$this->error('请上传图片');
		}
		//保存附件信息
		$model=M("Attach");
		$data=array(
				'record_id'	=> 	$_REQUEST['record_id'],
				'name'		=>	$_POST['name'],
				'path'		=> 	$image,
				'model' 	=> 	'Article',
				'size'		=> 	getFileSize($image),
				'is_pic'	=>	1,
				'sort_order'=>	intval($_POST['sort_order']),
				'add_time'	=>	time()
		);
		if(isset($image)){
			$model->data($data)->add();
		}else{
			$this->error('图片上传失败！');
		}
		$this->success('添加成功!',$this->getReturnUrl().'&record_id='.$_REQUEST['record_id']);
	}
	
	public function edit() {
        //当前数据
        $model=M("Attach");
        $id = $_REQUEST[$model->getPk()];
        $data = $model->find($id);
        $this->assign('data', $data);
        $this->display('add');
    }
	
    public function update(){
    	if(empty($_POST['name'])){
			$this->error('请填写图片名称！');
		}
    	$model=M("Attach");
    	//create与save中间不能用model操作数据库
    	$pic=$model->getById($_POST['id']);
    	//若有上传信息
    	if(!empty($_FILES['image']['name'])){
    		$info=$this->_upload($this->getSavePath($_FILES['image']['name'],3,'Article'),false);
    		if(isset($info[0]['savename'])){
    			//成功后删除原来图片
    			@unlink($pic['path']);
    			$image = $info[0]['savepath'].$info[0]['savename'];
    		}else{
    			$image=	$pic['path'];
    		}
    	}else{
    		$image=$pic['path'];
    	}
    	//更新附件信息
    	$data['name']	=	$_POST['name'];
    	$data['id']	= $_POST['id'];
    	$data['path']=$image;
    	$data['sort_order']=intval($_POST['sort_order']);
    	$data['size']=getFileSize($image);
    	$data['add_time']=time();
    	if(isset($image)){
    		$model->data($data)->save();
    	}
    	$this->success('编辑成功!',$this->getReturnUrl().'&record_id='.$_REQUEST['record_id']);
    }
    
	/**
	 * Ajax删除
	 */
	public function ajax_delete() {
        //删除指定记录
        $model = M($this->getActionName());
        if (!empty($model)) {
            $pk = $model->getPk();
            $id = $_REQUEST[$pk];
            if (isset($id)) {
                $condition = array($pk => array('in', explode(',', $id)));
            	$images=$model->where($condition)->select();
                if (false !== $model->where($condition)->delete()) {
	                if(count($images)>0){
	    				foreach ($images as $v){
	    					@unlink($v['path']);
	    				}
	    			}
                    $this->ajaxReturn(1,'删除成功！',1);
                }else{
					 $this->ajaxReturn(0,'删除失败！',0);
				}
            }else{
				 $this->ajaxReturn(0,'非法操作！',0);
			}
        }
    }
	
}

?>